package com.pureut.order.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.order.dto.CustomerSaleRankDto;
import com.pureut.order.dto.SalesOrderDto;
import com.pureut.order.dto.SalesOrderViewDto;
import com.pureut.order.service.SalesOrderService;
import com.pureut.order.vo.*;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.order.api.dto.SaleServiceDto;
import com.sale.order.api.vo.SaleServiceVo;
import com.sale.order.api.vo.UpdateSaleOrderVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * 销售订单控制层
 * Author:C
 * Date:2022/12/01 9:13
 * @DESC
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sales-order")
public class SalesOrderController extends BaseController {


    private final SalesOrderService salesOrderService;

    /**
     * 销售订单列表
     */
    //@SaCheckPermission("order:saleOrder:list")
    @GetMapping("/list")
    public TableDataInfo<Map<String,String>> list(SalesOrderVo salesOrderVo, PageQuery pageQuery) {
        //查询销售订单列表
        return salesOrderService.getList(salesOrderVo, pageQuery);
    }

    /**
     * 新增销售订单
     */
    //@SaCheckPermission("order:saleOrder:add")
    @Log(title = "新增销售订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody Map<String,Object> map) throws Exception {
        return toAjax(salesOrderService.addSalesOrder(map));
    }

    /**
     * 提交评审
     */
    //@SaCheckPermission("order:saleOrder:examine")
    @Log(title = "提交评审", businessType = BusinessType.UPDATE)
    @GetMapping("/submitReview/{id}")
    public R<Void> submitReview(@PathVariable("id") Long id) {
        return toAjax(salesOrderService.submitReview(id));
    }


    /**
     * 审核撤销
     */
    //@SaCheckPermission("order:sale:saleOrder:revoke")
    @Log(title = "审核撤销", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteReview/{id}")
    public R<Void> deleteReview(@PathVariable("id") Long id) {
        return toAjax(salesOrderService.deleteReview(id));
    }

    /**
     * 手动关结
     */
    //@SaCheckPermission("order:saleOrder:manualClosing")
    @Log(title = "手动关结", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public R<Void> close(@RequestBody SalesOrderVo salesOrderVo) {
        return toAjax(salesOrderService.close(salesOrderVo));
    }

    /**
     * 批量删除
     */
    //@SaCheckPermission("order:saleOrder:remove")
    @Log(title = "批量删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public R<Void> delete(@PathVariable("ids") String ids) {
        return toAjax(salesOrderService.delete(ids));
    }

    /**
     * 修改
     */
    //@SaCheckPermission("order:saleOrder:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Void> update(@Validated @RequestBody Map<String,Object> map) throws SQLException {
        return toAjax(salesOrderService.updateSalesOrder(map));
    }

    /**
     * 查看
     */
    //@SaCheckPermission("order:saleOrder:query")
    @GetMapping("/view/{id}")
    public R<Map<String,Object>> view(@PathVariable("id") Long id) {
        return R.ok(salesOrderService.getView(id));
    }

    /**
     * 合同上传
     */
    //@SaCheckPermission("order:saleOrder:contractUp")
    @Log(title = "合同上传", businessType = BusinessType.UPDATE)
    @PostMapping("/contractUpload")
    public R<Void> contractUpload(@Validated @RequestBody ContractUploadVo contractUploadVo) throws Exception {
        return toAjax(salesOrderService.contractUpload(contractUploadVo));
    }

    /**
     * 销售订单审核
     */
    //@SaCheckPermission("order:saleOrder:examine")
    @Log(title = "销售订单审核", businessType = BusinessType.UPDATE)
    @PostMapping("/saleOrderReview")
    public R<Void> saleOrderReview(@Validated @RequestBody OrderReviewVo orderReviewVo) throws Exception {
        return toAjax(salesOrderService.saleOrderReview(orderReviewVo));
    }

    /**
     * 获取销售订单明细中最新的信息集合
     */
    @GetMapping("/getNewSaleMap")
    public Map<String, Date> getNewSaleMap() {
        return salesOrderService.getNewSaleMap();
    }

    /**
     * 获取销售订单列表(其他模块调用)
     */
    @GetMapping("/getSaleOrderList")
    public TableDataInfo<SalesOrderDto> getSaleOrderList(SalesOrderVo salesOrderVo, PageQuery pageQuery) {
        //查询销售订单列表
        return salesOrderService.getSaleOrderList(salesOrderVo, pageQuery);
    }

    /**
     * 销售发货回传重量（dubbo 调用）
     */
    @PostMapping("/updateSaleOrder")
    public R<Void> updateSaleOrder(@RequestBody UpdateSaleOrderVo updateSaleOrderVo) {
        return toAjax(salesOrderService.updateSaleOrder(updateSaleOrderVo));
    }

//    /**
//     * 排程模块回传数据，修改销售订单状态及数据（feign调用）
//     */
//    @PutMapping("/updateDataByScheduling")
//    public R<?> updateDataByScheduling(@RequestBody UpdateDataBySchedulingVo updateDataBySchedulingVo) {
//        return R.ok(salesOrderService.updateDataByScheduling(updateDataBySchedulingVo));
//    }

    /**
     * 根据销售订单号及物料编码获取明细信息
     */
    @GetMapping("/getDataBySaleNumberAndMaterialCode")
    public R<?> getDataBySaleNumberAndMaterialCode(String saleNumber, String materialCode) {
        return R.ok(salesOrderService.getDataBySaleNumberAndMaterialCode(saleNumber, materialCode));
    }

    /**
     * 根据销售订单修改对应明细状态（生产计划 feign调用）
     */
    @PostMapping("/updateSaleOrderDetailStatus")
    public void updateSaleOrderDetailStatus(@RequestBody UpdateSaleOrderDetailStatusVo updateSaleOrderDetailStatusVo) {
        salesOrderService.updateSaleOrderDetailStatus(updateSaleOrderDetailStatusVo);
    }

    /**
     * 成品称重页面回传数据至销售订单(成品称重 feign调用)
     */
    @PostMapping("/backData")
    public void backData(@RequestBody BackDataVo backDataVo) {
        salesOrderService.backData(backDataVo);
    }

    /**
     * Feign销售发货重量回传
     */
    @PostMapping("/updateShippingWeight")
    public boolean updateShippingWeight(@RequestBody BackDataVo backDataVo) {
        return salesOrderService.updateShippingWeight(backDataVo);
    }

    /**
     * 销售发货扣除重量（dubbo 调用）
     *
     * @param backDataVo
     * @return
     */
    @PostMapping("/updateShippingWeightSub")
    public boolean updateShippingWeightSub(@RequestBody BackDataVo backDataVo) {
        return salesOrderService.updateShippingWeightSub(backDataVo);
    }

    /**
     * Fegin销售退货重量回传
     *
     * @param backDataVo
     * @return
     */
    @PostMapping("/updateShippingWeightBack")
    public boolean updateShippingOutWeight(@RequestBody BackDataVo backDataVo) {
        return salesOrderService.updateShippingOutWeight(backDataVo);
    }

    /**
     * feign销售发货退货重量回传（扣减）
     *
     * @param backDataVo
     * @return
     */
    @PostMapping("/updateShippingWeightSubBack")
    public boolean updateShippingWeightSubBack(@RequestBody BackDataVo backDataVo) {
        return salesOrderService.updateShippingWeightSubBack(backDataVo);
    }

    /**
     * 委外入库回传销售订单明细委外重量(委外入库fegin调用)
     */
    @PostMapping("/updateSubWeight")
    public boolean updateSubWeight(@RequestBody BackDataVo backDataVo) {
        return salesOrderService.updateSubWeight(backDataVo);
    }

    /**
     * 根据销售订单查出物料的单价（服务调用）
     */
    @GetMapping("/getSalePlace")
    public SaleServiceDto getSalePlace(@RequestBody SaleServiceVo saleServiceVo) {
        return salesOrderService.getSalePlace(saleServiceVo);
    }

    /**
     * 销售订单数(首页工作台使用)
     */
    @GetMapping("/getSaleOrderNum")
    public String getSaleOrderNum() {
        return salesOrderService.getSaleOrderNum();
    }

    /**
     * 销售订单金额（首页工作台使用）
     */
    @GetMapping("/getSaleOrderPrice")
    public String getSaleOrderPrice() {
        return salesOrderService.getSaleOrderPrice();
    }

    /**
     * 前五大客户销售占比(元)
     */
    @GetMapping("/customerSaleRank")
    public R<List<CustomerSaleRankDto>> customerSaleRank() {
        return R.ok(salesOrderService.customerSaleRank());
    }

}
